package com.extension.company.view.home.hotactivity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.ScrollView;
import android.widget.TextView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.extension.company.admin.R;
import com.extension.company.common.mvp.BaseFragment;
import com.extension.company.common.utils.ClipboardUtils;
import com.extension.company.common.utils.ToastUtil;
import com.extension.company.common.widget.ShapedImageView;
import com.extension.company.common.widget.SlideDetailsLayout;
import com.extension.company.common.widget.popuwindow.SharePw;
import com.extension.company.contract.home.GoodInfoFragmentContract;
import com.extension.company.model.entity.GoodsInfoDetail2;
import com.extension.company.model.entity.GoodsInforBean;
import com.extension.company.presenter.home.GoodInfoFragmentPresenter;
import com.extension.company.view.details.RecommendGoodsBean;
import com.extension.company.view.details.SecondFloorFragment;
import com.extension.company.view.details.SecondFloorTroubleFragment;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.wuhenzhizao.sku.bean.Product;
import com.wuhenzhizao.sku.bean.Sku;
import com.wuhenzhizao.sku.bean.SkuAttribute;
import com.wuhenzhizao.sku.use.ProductSkuDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 商品详情页Fragment
 */
public class GoodsInfoFragment extends BaseFragment<GoodInfoFragmentContract.View, GoodInfoFragmentPresenter>
		implements GoodInfoFragmentContract.View, SlideDetailsLayout.OnSlideDetailsListener {

	@BindView(R.id.fab_up_slide)
	FloatingActionButton fab_up_slide;// 底部的floatButton
	@BindView(R.id.sv_switch)
	SlideDetailsLayout sv_switch;//左右滑动的tab的布局
	@BindView(R.id.sv_goods_info)
	ScrollView sv_goods_info; // tab布局中scrollview
	@BindView(R.id.v_tab_cursor)
	View v_tab_cursor; // 商品详情第二个界面的fragment 中的tab 底部的横线
	@BindView(R.id.vp_item_goods_img)
	ConvenientBanner convenientBanner; // banner
	@BindView(R.id.fl_content)
	FrameLayout fl_content;// 商品详情第二个fragment的content容器
	@BindView(R.id.ll_select_goods_type)
	LinearLayout ll_select_goods_type;// 请选择套餐
	@BindView(R.id.ll_comment)
	LinearLayout ll_comment;
	@BindView(R.id.ll_pull_up)
	LinearLayout ll_pull_up;// 上拉效果，弹出第二个fragment
	@BindView(R.id.ll_goods_detail)
	LinearLayout ll_goods_detail; //商品行情
	@BindView(R.id.ll_goods_config)
	LinearLayout ll_goods_config; // 评论
	@BindView(R.id.tv_goods_detail)
	TextView tv_goods_detail; // 商品详情
	@BindView(R.id.tv_goods_config)
	TextView tv_goods_config; // 商品评论
	@BindView(R.id.tv_goods_title)
	TextView tv_goods_title;
	@BindView(R.id.tv_new_price)
	TextView tv_new_price;  // 现价
	@BindView(R.id.tv_old_price)
	TextView tv_old_price;   // 原价
	@BindView(R.id.tv_current_goods)
	TextView tv_current_goods;  // x选择套餐
	@BindView(R.id.tv_comment_count)
	TextView tv_comment_count;
	@BindView(R.id.immediately_share)
	TextView immediatelyShare;
	@BindView(R.id.btn_buy_or_order)
	TextView btnBuyOrOrder;
	@BindView(R.id.goods_share_count)
	TextView goodsShareCount;

	private int i = 0;

	/**
	 * 当前商品详情数据页的索引分别是图文详情、规格参数
	 */
	private int nowIndex;
	private float fromX;
	public SecondFloorTroubleFragment secondFloorTroubleFragment;
	public SecondFloorFragment secondFloorFragment;
	private Fragment nowFragment;
	private List<TextView> tabTextList;// 	tab标签列表
	private List<Fragment> fragmentList = new ArrayList<>(); // 商品、 评价的列表
	private List<String> imgUrls = new ArrayList<>(); // Banner加载的图片地址
	private FragmentTransaction fragmentTransaction;
	private FragmentManager fragmentManager;
	public HotActivityDetailActivity activity;
	private LayoutInflater inflater;
	private int type;
	private SharePw sharePw;
	private Context context;

	private List<GoodsInforBean.DataBean.GoodsInfoBean.SpecDataBean.SpecAttrBean> standardsList;
	private List<GoodsInforBean.DataBean.GoodsInfoBean.SpecDataBean.SpecListBean> standardInfoList;
	private GoodsInfoDetail2 bean;
	private boolean hasStores = false; // 无库存

	private ProductSkuDialog dialog;
	private int shoppingCartNum = 0;

	@Override
	public void onAttach(Context context) {
		super.onAttach(context);
		this.activity = (HotActivityDetailActivity) context;
		this.context = context;
	}

	// 开始自动翻页
	@Override
	public void onResume() {
		super.onResume();
		convenientBanner.startTurning(4000);    //开始自动翻页，banner设置
	}

	// 停止自动翻页，banner设置
	@Override
	public void onPause() {
		super.onPause();
		convenientBanner.stopTurning();    //停止翻页
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		UMShareAPI.get(activity).release(); // 解决友盟分享内存泄漏的问题
	}

	@Override
	public GoodInfoFragmentContract.View createView() {
		return this;
	}

	@Override
	public GoodInfoFragmentPresenter createPresenter() {
		return new GoodInfoFragmentPresenter();
	}

	@Override
	public int getMainLayout() {
		return R.layout.fragment_goods_info;
	}

	@Override
	public void init() {
		type = getArguments().getInt("type");
		int goodId = getArguments().getInt("goodid");

		presenter.goodsAndCommitRequest(goodId); // 根据商品的id，获取信息
		initView();
		initData();
	}


	private void initView() {
		setDetailData();
		initBannerView();
//		setRecommendGoods();

		//设置文字中间一条横线
		tv_old_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
		fab_up_slide.hide(); // 右下角一键到达顶部的floatButton的隐藏

		//设置指示器 /指示器的排列顺序
		convenientBanner.setPageIndicator(new int[]{R.drawable.index_white, R.drawable.index_red});
		convenientBanner.setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL);
	}

	private void initData() {
		fragmentList = new ArrayList<>();
		tabTextList = new ArrayList<>();

		tabTextList.add(tv_goods_detail);
		tabTextList.add(tv_goods_config);
		//presenter.addGoods2Cart(); // 添加到购物车的方法
	}

	/**
	 * 加载完商品详情执行
	 */
	public void setDetailData() {
		if (type == 0) {
			btnBuyOrOrder.setText("立即预约");
		} else {
			btnBuyOrOrder.setText("立即购买");
		}
		secondFloorFragment = new SecondFloorFragment(); // 商品详情页的上拉第二页，商品详情
		secondFloorTroubleFragment = new SecondFloorTroubleFragment(); //商品详情的上拉第二页的常见故障

		fragmentList.add(secondFloorTroubleFragment);
		fragmentList.add(secondFloorFragment);

		nowFragment = secondFloorFragment;  // 当前fragment
		fragmentManager = getChildFragmentManager();  // fragment中的子fragment的管理者
		//默认显示商品详情tab，第二个商品详情界面
		fragmentManager.beginTransaction().replace(R.id.fl_content, nowFragment).commitAllowingStateLoss();
	}

	@Override
	public void showLoading() {

	}

	@Override
	public void hideLoading() {

	}

	@Override
	public void onError(Throwable throwable) {

	}

	@Override
	public void goodsCommit() { // 评论

	}

	@Override
	public void goodsInfo(GoodsInforBean.DataBean.GoodsInfoBean bean) { // 商品详情
		tv_goods_title.setText(bean.getGoods_name()); // 商品名
		goodsShareCount.setText(String.format(getResources().getString(R.string.goods_share_count), bean.getShare_count()));
	}

	@Override
	public void banner(List<String> urlList) { // 轮播图
		imgUrls.addAll(urlList);
		initBannerView();

	}

	@Override
	public void addCarMsgCallBack(String msg) { //添加购物车，返回的信息
		ToastUtil.showToast(activity, msg);
	}

	@Override
	public void secondFloorFragment() {

	}

	@Override
	public void onStatucChanged(SlideDetailsLayout.Status status) {  // 详情页分页展示的监听事件
		if (status == SlideDetailsLayout.Status.OPEN) {  // 商品详情页
			fab_up_slide.show(); // 右下角到达顶部的按钮的显示
			activity.vpContent.setNoScroll(true); // 设置viewpager是否可以左右滑动
			activity.tvTitle.setVisibility(View.VISIBLE); // 详情页的标题显示
			activity.pstsTabs.setVisibility(View.GONE); // 详情页的情况下，tablayout 不显示
		} else {     // 商品的二楼分页
			fab_up_slide.hide();  // 右下角到达顶部的按钮的隐藏
			activity.vpContent.setNoScroll(false);  // 在二楼的情况下，设置viewPager 不可以左右的滑动
			activity.tvTitle.setVisibility(View.GONE); // 二楼的情况下详情页的标题不显示
			activity.pstsTabs.setVisibility(View.VISIBLE); // 二楼的情况下，tablayout 显示
		}
	}

	/**
	 * 滑动游标
	 */
	private void scrollCursor() {
		TranslateAnimation anim = new TranslateAnimation(fromX, nowIndex * v_tab_cursor.getWidth(), 0, 0);
		anim.setFillAfter(true);//设置动画结束时停在动画结束的位置
		anim.setDuration(50);
		//保存动画结束时游标的位置,作为下次滑动的起点
		fromX = nowIndex * v_tab_cursor.getWidth();
		v_tab_cursor.startAnimation(anim);

		//设置Tab切换颜色
		for (int i = 0; i < tabTextList.size(); i++) {
			tabTextList.get(i).setTextColor(i == nowIndex ? getResources().getColor(R.color.text_red) : getResources().getColor(R.color.text_black));
		}
	}

	/**
	 * 切换Fragment
	 * <p>(hide、show、add)
	 *
	 * @param fromFragment 当前的fragment
	 * @param toFragment   要跳转到的fragment
	 */
	private void switchFragment(Fragment fromFragment, Fragment toFragment) {
		if (nowFragment != toFragment) {
			fragmentTransaction = fragmentManager.beginTransaction();
			if (!toFragment.isAdded()) {    // 先判断是否被add过
				fragmentTransaction.hide(fromFragment).add(R.id.fl_content, toFragment).commitAllowingStateLoss(); // 隐藏当前的fragment，add下一个到activity中
			} else {
				fragmentTransaction.hide(fromFragment).show(toFragment).commitAllowingStateLoss(); // 隐藏当前的fragment，显示下一个
			}
		}
	}

	/**
	 * 处理推荐商品数据(每两个分为一组)
	 *
	 * @param data
	 * @return
	 */
	public static List<List<RecommendGoodsBean>> handleRecommendGoods(List<RecommendGoodsBean> data) {
		List<List<RecommendGoodsBean>> handleData = new ArrayList<>();
		int length = data.size() / 2;
		if (data.size() % 2 != 0) {
			length = data.size() / 2 + 1;
		}
		for (int i = 0; i < length; i++) {
			List<RecommendGoodsBean> recommendGoods = new ArrayList<>();
			for (int j = 0; j < (i * 2 + j == data.size() ? 1 : 2); j++) {
				recommendGoods.add(data.get(i * 2 + j));
			}
			handleData.add(recommendGoods);
		}
		return handleData;
	}

	@OnClick({R.id.ll_pull_up, R.id.ll_select_goods_type, R.id.fab_up_slide, R.id.ll_goods_detail,
			R.id.ll_goods_config, R.id.btn_buy_or_order, R.id.immediately_share})
	public void onViewClicked(View view) {
		switch (view.getId()) {
			case R.id.ll_pull_up:
				//上拉查看图文详情
				sv_switch.smoothOpen(true);
				break;
			case R.id.ll_select_goods_type: // 选择商品的规格
				if (null == bean.getData().getSpecData()) { //如果是单规格
					immediateBuy(view);
				} else {  // 如果是多规格
					showSkuDialog();
				}
				break;

			case R.id.fab_up_slide:
				//点击滑动到顶部
				sv_goods_info.smoothScrollTo(0, 0);
				sv_switch.smoothClose(true);
				break;

			case R.id.ll_goods_detail:  // 二楼商品详情
				//商品详情tab
				nowIndex = 0;
				scrollCursor();
				switchFragment(nowFragment, secondFloorFragment);
				nowFragment = secondFloorFragment;
				break;

			case R.id.ll_goods_config: // 二楼的常见问题
				//规格参数tab
				nowIndex = 1;
				scrollCursor(); //  点击后，底部的油标滑动
				switchFragment(nowFragment, secondFloorTroubleFragment);
				nowFragment = secondFloorTroubleFragment;
				break;
			case R.id.immediately_share:  // 分享
				UMShare();
				break;
			case R.id.btn_buy_or_order:  // 立即购买
				if (null == bean.getData().getSpecData()) { //如果是单规格
					immediateBuy(view);
				} else {  // 如果是多规格
					showSkuDialog();
				}

				break;
		}
	}

	/**
	 * 轮播图的相关的设置
	 */
	public void initBannerView() {
		convenientBanner.setPages(new CBViewHolderCreator() {
			@Override
			public Holder createHolder(View itemView) {
				return new NetworkImageHolderView(itemView);
			}

			@Override
			public int getLayoutId() {
				return R.layout.item_recommend_goods_item;
			}
		}, imgUrls);
	}

	/**
	 * banner加载网络图片的方法
	 */
	public class NetworkImageHolderView extends Holder<String> {
		private ImageView imgeView;

		public NetworkImageHolderView(View itemView) {
			super(itemView);
		}

		@Override
		protected void initView(View itemView) {
			imgeView = itemView.findViewById(R.id.sdv_goods); // banner item imageView
		}

		@Override
		public void updateUI(String data) {
			Glide.with(activity).load(data).listener(new RequestListener<Drawable>() {
				@Override
				public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
					Log.d("zwk", "zwkonException: " + e.toString() + "  model:" + model + " isFirstResource: " + isFirstResource);
					return false;
				}

				@Override
				public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
					Log.e("zwk", "model:" + model + " isFirstResource: " + isFirstResource);
					return false;
				}
			}).into(imgeView);
		}
	}

	/**
	 * 分享
	 */
	private void UMShare() {
		sharePw = new SharePw(activity, new SharePw.clikcType() {
			@Override
			public void callBackType(String shareMedia) {
				SHARE_MEDIA share_media = null;
				switch (shareMedia) {
					case "WEIXIN":
						share_media = SHARE_MEDIA.WEIXIN;
						break;
					case "WEIXIN_CIRCLE":
						share_media = SHARE_MEDIA.WEIXIN_CIRCLE;
						break;
					case "COPY_LINK":
						ClipboardUtils.copyText(activity, "链接已经复制了");
						ToastUtil.showToast(activity, "链接已复制到粘贴板");
						break;
					default:
						break;

				}

				UMWeb web = new UMWeb("http://mobile.umeng.com/social"); // 分享链接的方法
				web.setTitle("来自分享面板标题");   // 链接的标题
				web.setDescription("来自分享面板内容");  //链接的描述
				web.setThumb(new UMImage(activity, R.drawable.xiaomi));  // 链接的缩略图
				new ShareAction(activity).withMedia(web)    // 将分享链接的参数 设置到分享中
						.setPlatform(share_media) // 设置分享的平台
						.setCallback(new UMShareListener() {
							@Override
							public void onStart(SHARE_MEDIA share_media) {  // 分享开始

							}

							@Override
							public void onResult(SHARE_MEDIA share_media) { // 分享成功的 结果
								sharePw.dismiss();

							}

							@Override
							public void onError(SHARE_MEDIA share_media, Throwable throwable) {  // 分享失败

							}

							@Override
							public void onCancel(SHARE_MEDIA share_media) { // 取消分享

							}
						})
						.share();


			}
		});

		sharePw.show();
	}


	//----------------------------------------------------------------------sku算法 多规格------------------------------------

	@Override
	public void p2v(GoodsInfoDetail2 bean2) {
		this.bean = bean2;
	}

	/**
	 * 使用展示多规格的dialog对话框
	 */
	private void showSkuDialog() {
		if (dialog == null) {
			dialog = new ProductSkuDialog(context);
			// 填充后台返回的数据Product.get(this)
			dialog.setData(turnData(), new ProductSkuDialog.Callback() {
				@Override
				public void onAdded(Sku sku, int quantity) {
					Intent orderConfirmIntent = new Intent(context, OnlineWaitForPayActivity.class);
					orderConfirmIntent.putExtra("orderInfo", sku);
					orderConfirmIntent.putExtra("orderNum", quantity);
					context.startActivity(orderConfirmIntent);
					shoppingCartNum += quantity;
				}
			});
		}
		dialog.show();
	}

	/**
	 * 后台传过来的数据进行二次的整合封装
	 *
	 * @return product对象
	 */
	private Product turnData() {
		List<GoodsInfoDetail2.DataBean.SpecDataBean.SpecListBean> goodsList = bean.getData().getSpecData().getSpec_list(); // 后台传的商品列表
		List<GoodsInfoDetail2.DataBean.SpecDataBean.SpecAttrBean> goodsAttrList = bean.getData().getSpecData().getSpec_attr();//后台传给的商品规格列表
		Product product = new Product();
		product.setId(String.valueOf(bean.getData().getGoods_info().getGoods_id()));
		product.setName(bean.getData().getGoods_info().getGoods_name());
		product.setMainImage(bean.getData().getGoods_info().getImage());
		product.setSellingPrice(bean.getData().getGoods_info().getSales_actual());
		product.setOriginPrice(bean.getData().getGoods_info().getSales_initial());
		product.setCurrencyUnit("¥");
		product.setMeasurementUnit("件");
		product.setSaleQuantity(bean.getData().getGoods_info().getGoods_sort());
		product.setStockQuantity(bean.getData().getGoods_info().getGoods_sort());
		List<Sku> skuList = new ArrayList<>();
		for (int i = 0; i < goodsList.size(); i++) { // 真正的数据
			Sku sku = new Sku();
			sku.setId(String.valueOf(bean.getData().getGoods_info().getGoods_id()));
			sku.setGoodsName(bean.getData().getGoods_info().getGoods_name());
			sku.setGoodsSpecSkuId(String.valueOf(goodsList.get(i).getSpec_sku_id()));
			sku.setMainImage(goodsList.get(i).getForm().getImgshow());
			sku.setSellingPrice(Long.parseLong(goodsList.get(i).getForm().getLine_price().substring(0, goodsList.get(i).getForm().getLine_price().indexOf("."))));
			sku.setOriginPrice(Long.parseLong(goodsList.get(i).getForm().getGoods_price().substring(0, 1)));
			sku.setStockQuantity(goodsList.get(i).getForm().getStock_num());
			sku.setStoreImage("");
			sku.setStoreName("");
			List<SkuAttribute> skuAttributeList = new ArrayList<>();
			String[] idArray = goodsList.get(i).getSpec_sku_id().split("_");
			for (int j = 0; j < goodsAttrList.size(); j++) { // 规格组grouP
				for (int k = 0; k < goodsAttrList.get(j).getSpec_items().size(); k++) { // 规格组item
					for (int n = 0; n < idArray.length; n++) { // 真正数据的id进行循环判断
						if (Integer.valueOf(idArray[n]) == goodsAttrList.get(j).getSpec_items().get(k).getItem_id()) {
							SkuAttribute skuAttribute = new SkuAttribute();
							skuAttribute.setKey(goodsAttrList.get(j).getGroup_name());
							skuAttribute.setValue(goodsAttrList.get(j).getSpec_items().get(k).getSpec_value());
							skuAttributeList.add(skuAttribute);
						}
					}
				}
			}
			sku.setAttributes(skuAttributeList);
			skuList.add(sku);
		}
		product.setSkus(skuList);
		return product;
	}

	//---------------------------单规格---------------------------------------------

	/**
	 * 购买/选择规格的popwindow
	 *
	 * @param view
	 */
	private void immediateBuy(View view) {
		// 点击打开popWindow 界面
		View popwindowViewBuy = LayoutInflater.from(activity).inflate(R.layout.popwindow_buy, null); // popwindow界面
		final PopupWindow popupWindow = new PopupWindow(popwindowViewBuy, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
		popupWindow.setAnimationStyle(R.style.popwindow_anim_style);
		popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
		WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
		lp.alpha = 0.7f;
		activity.getWindow().setAttributes(lp);
		popupWindow.setFocusable(true);
		popupWindow.setOutsideTouchable(true);
		popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
		LinearLayout groupLinearLayout = popwindowViewBuy.findViewById(R.id.specifaction_group); // 总布局
		ImageView imgClose = popwindowViewBuy.findViewById(R.id.windowpopmenu_dismiss); // 关闭
		final TextView introduct = popwindowViewBuy.findViewById(R.id.commodity_introduce);  // 商品的简介
		final TextView price = popwindowViewBuy.findViewById(R.id.commodity_price); // 价格
		final ShapedImageView roundImg = popwindowViewBuy.findViewById(R.id.goods_img);// 商品头像
		ImageButton cut = popwindowViewBuy.findViewById(R.id.cut); // 减数字
		ImageButton add = popwindowViewBuy.findViewById(R.id.add); // 加数字
		final TextView buyNum = popwindowViewBuy.findViewById(R.id.buy_number); // 数量
		Button addToCart = popwindowViewBuy.findViewById(R.id.immediately_add_car); //加入购物车
		final Button buy = popwindowViewBuy.findViewById(R.id.immediately_buy);// 立即购买
		popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { // 关闭popwindow的监听事件
			@Override
			public void onDismiss() {
				WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
				lp.alpha = 1.0f;
				activity.getWindow().setAttributes(lp);
			}
		});

		imgClose.setOnClickListener(new View.OnClickListener() { // 右上角的关闭按钮
			@Override
			public void onClick(View v) {
				popupWindow.dismiss();
				WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
				lp.alpha = 1.0f;
				activity.getWindow().setAttributes(lp);
			}
		});

		cut.setOnClickListener(new View.OnClickListener() {  // 数字减法
			@Override
			public void onClick(View v) {
				--i;
				if (i <= 1) {
					i = 1;
				}
				buyNum.setText("" + i);
			}
		});
		add.setOnClickListener(new View.OnClickListener() {   // 数字加法
			@Override
			public void onClick(View v) {

				++i;
				buyNum.setText("" + i);
			}
		});

		addToCart.setOnClickListener(new View.OnClickListener() {  // 加入购物车
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(activity, OnlineWaitForPayActivity.class);
				startActivity(intent);
			}
		});
		buy.setOnClickListener(new View.OnClickListener() { // 立即购买
			@Override
			public void onClick(View v) {
				Intent orderConfirmIntent = new Intent(activity, OnlineWaitForPayActivity.class);
				Sku sku = new Sku();  // 单规格和多规格数据不同，所以单规格按照双规格进行数据的封装，传递
				sku.setGoodsName(bean.getData().getGoods_info().getGoods_name());
				sku.setId(String.valueOf(bean.getData().getGoods_info().getGoods_id()));
				sku.setMainImage(bean.getData().getGoods_info().getImage());
				sku.setSellingPrice(bean.getData().getGoods_info().getSales_actual());

				orderConfirmIntent.putExtra("orderInfo", sku);
				orderConfirmIntent.putExtra("orderNum", buy.getText().toString().trim());
				startActivity(orderConfirmIntent);
				if (popupWindow.isShowing()) {
					popupWindow.dismiss();
				}
			}
		});

		introduct.setText(String.format(getResources().getString(R.string.goos_introduct_info),
				bean.getData().getGoods_info().getGoods_name(),
				bean.getData().getGoods_info().getSpec().get(0).getStock_num()));  // 设置商品得名称和库存量
		price.setText(bean.getData().getGoods_info().getSpec().get(0).getGoods_price());
		Glide.with(activity).load(bean.getData().getGoods_info().getImage()).into(roundImg);
	}


}

